<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>PyMOTW: getpass &mdash; PyMOTW Document v1.6 documentation</title>
    <link rel="stylesheet" href="../_static/mytest.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.6',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    
    <!-- comment utility -->
    <link rel="stylesheet" href="../_static/comment.css" type="text/css" />
    <script type="text/javascript" src="../_static/md5.js"></script>
    <script type="text/javascript" src="../_static/comment.js"></script>
    <!-- end -->
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="PyMOTW Document v1.6 documentation" href="../index.html" />
    <link rel="next" title="PyMOTW: string" href="string.html" />
    <link rel="prev" title="PyMOTW: array" href="array.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="string.html" title="PyMOTW: string"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="array.html" title="PyMOTW: array"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">PyMOTW Document v1.6 documentation</a> &raquo;</li> 
      </ul>
    </div> 
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">PyMOTW: getpass</a><ul>
<li><a class="reference external" href="#id1">例子</a></li>
<li><a class="reference external" href="#getpass">在非终端中使用getpass</a></li>
<li><a class="reference external" href="#id2">参考</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="array.html"
                                  title="previous chapter">PyMOTW: array</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="string.html"
                                  title="next chapter">PyMOTW: string</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/documents/getpass.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div> 

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="pymotw-getpass">
<h1>PyMOTW: getpass<a class="headerlink" href="#pymotw-getpass" title="Permalink to this headline">¶</a></h1>
<ul class="simple">
<li>模块： getpass</li>
<li>目的： 提示用户输入一个值(通常为密码), 输入时不显示内容.</li>
<li>python版本： 1.5.2</li>
</ul>
<p>许多通过终端与用户交互的程序需要向用户询问密码而在用户输入时不将其内容显示出来, getpass这个模块提供了一种简单的方法来安全地处理这个密码输入问题.</p>
<div class="section" id="id1">
<h2>例子<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">getpass()</span></tt> 函数提示用户输入, 并获取用户回车之前的内容. 输入内容以字符串的形式回传给调用者.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">getpass</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">()</span>
<span class="k">print</span> <span class="s">&#39;You entered:&#39;</span><span class="p">,</span> <span class="n">p</span>
</pre></div>
</div>
<p>如果调用者没有指定提示内容, 默认为&#8221;Password:&#8221;.</p>
<div class="highlight-python"><pre>$ python getpass_defaults.py
Password:
You entered: sekret</pre>
</div>
<p>当然提示可以是当然提示可以是程序所需要的任何内容.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">getpass</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">(</span><span class="n">prompt</span><span class="o">=</span><span class="s">&#39;What is your favorite color? &#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;blue&#39;</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;Right. Off you go.&#39;</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;Auuuuugh!&#39;</span>
</pre></div>
</div>
<p>我不推荐使用这种不安全的认证方式, 但它仅仅只是为了说明这一点.</p>
<div class="highlight-python"><pre>$ python getpass_prompt.py
What is your favorite color?
Right. Off you go.
$ python getpass_prompt.py
What is your favorite color?
Auuuuugh!</pre>
</div>
<p>默认情况下, <tt class="docutils literal"><span class="pre">getpass()</span></tt> 在stdout中显示提示字符串. 程序可以产生有用的信息到sys.stdout, 那么也可以将提示发到另一个流中, 比如sys.stderr.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">getpass</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">(</span><span class="n">stream</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;You entered:&#39;</span><span class="p">,</span> <span class="n">p</span>
</pre></div>
</div>
<p>这种方式将标准输出重定向到一个pipe或者文件, 这种方式将标准输出重定向到一个pipe或者文件, 因而不显示密码提示. 用户输入的值同样不会回显到屏幕上.</p>
<div class="highlight-python"><pre>$ python getpass_stream.py &gt;/dev/null
Password:</pre>
</div>
</div>
<div class="section" id="getpass">
<h2>在非终端中使用getpass<a class="headerlink" href="#getpass" title="Permalink to this headline">¶</a></h2>
<p>在Unix环境中, <tt class="docutils literal"><span class="pre">getpass()</span></tt> 总是需要一个通过termios控制的tty, 因此可以控制密码不回显. 这也意味着无法从一个重定向到标准输入流的非终端流中获取输入.</p>
<div class="highlight-python"><pre>$ echo "sekret" | python getpass_defaults.py
Traceback (most recent call last):
File "getpass_defaults.py", line 34, in
  p = getpass.getpass()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/getpass.py", line 32, in unix_getpass
  old = termios.tcgetattr(fd) # a copy to save
termios.error: (25, 'Inappropriate ioctl for device')</pre>
</div>
<p>由调用者决定是否需要确定输入流是否为一个tty, 以及在非tty时使用备选方法.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">getpass</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">isatty</span><span class="p">():</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">(</span><span class="s">&#39;Using getpass: &#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;Using readline&#39;</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>

<span class="k">print</span> <span class="s">&#39;Read: &#39;</span><span class="p">,</span> <span class="n">p</span>
</pre></div>
</div>
<p>在tty中:</p>
<div class="highlight-python"><pre>$ python ./getpass_noterminal.py
Using getpass:
Read: sekret</pre>
</div>
<p>非tty环境:</p>
<div class="highlight-python"><pre>$ echo "sekret" | python ./getpass_noterminal.py
Using readline
Read: sekret</pre>
</div>
</div>
<div class="section" id="id2">
<h2>参考<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://docs.python.org/library/getpass.html">getpass</a></li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="string.html" title="PyMOTW: string"
             >next</a> |</li>
        <li class="right" >
          <a href="array.html" title="PyMOTW: array"
             >previous</a> |</li>
        <li><a href="../index.html">PyMOTW Document v1.6 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 2009, vbarter &amp; liz.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>